<template>
	<view>
		<view class="conterBg" style="background-color: #fff;">
			<view class="centerTxt f-t font-weight font-48">推广二维码</view>
			<view class="grey centerTxt font-28 m-t-10">欢迎推荐你的小伙伴前来使用</view>
			<view class="codeBox">
				<!-- <view class="" :style="{ backgroundImage: 'url(' + $util.img(QrCode) + ')' }"></view> -->
				<image :src="QrCodeLocal" mode="aspectFit"></image> 
			</view>
			<button  @click="preserveImg">保存二维码</button>
			<button  @click="$util.redirectTo('/otherpages/inviteList/inviteList')" class="m-t-60">我的邀请列表</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				QrCodeLocal: '/static/code.png' // 假设你的本地二维码图片位于项目的static文件夹下，并命名为qrcode.png
			};
		},
		onLoad(res) {
			this.getCode(res.memberno)
		},
		methods: {
			getCode(memberno) {
				this.$api.sendRequest({
					url: '/api/User/myqrcode',
					data: {
						page: `/pages/index/index`,
						qrcode_param: '{}'
					},
					success: res => {
						if (res.code == 1) {
							this.QrCode = res.data.qrcode
						}
					}
				})
			},




			getCodeImg() {
				// const cloud = require('wx-server-sdk')
				// cloud.init({
				//   env: cloud.DYNAMIC_CURRENT_ENV,
				// })
				// exports.main = async (event, context) => {
				//   try {
				//     const result = await cloud.openapi.wxacode.createQRCode({
				//         "path": 'page/index/index',
				//         "width": 430
				//       })
				// 	  console.log(result);
				//     return result
				//   } catch (err) {
				//     return err
				//   }
				// }
				uni.request({
					url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN',
					data: {

					},
				})
			},
			preserveImg() {
				let that = this
				//判断用户是否授权"保存到相册"
				wx.getSetting({
					success(res) {
						//没有权限，发起授权
						if (!res.authSetting['scope.writePhotosAlbum']) {
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									//用户允许授权，保存图片到相册
									that.imageFn();
								},
								fail() {
									//用户点击拒绝授权，跳转到设置页，引导用户授权
									wx.openSetting({
										success() {
											wx.authorize({
												scope: 'scope.writePhotosAlbum',
												success() {
													that.imageFn();
												}
											});
										}
									});
								}
							});
						} else {
							//用户已授权，保存到相册
							that.imageFn();
						}
					}
				});
			},
			imageFn() {
				// 保存图片到相册--网络地址
				console.log('保存图片到相册1')
				uni.downloadFile({
					url: this.$util.img(this.QrCode),
					success: res => {
						console.log('保存图片到相册2')
						const tempFilePath = res.tempFilePath
						uni.saveImageToPhotosAlbum({
							filePath: tempFilePath, //设置下载图片的地址
							success: function() {
								console.log("保存成功"); //保存成功后进行的提示
								uni.showToast({
									title: '保存成功',
									icon: 'success'
								})
							}
						})
					},
					err: err => {
						console.log(err, '+++---***--+++-*-*+9*-+*-+*+-*+56');
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ff5e0f;
	}

	.codeBox {
		width: 450rpx;
		height: 450rpx;
		margin: 110rpx auto;
		margin-bottom: 80rpx;
		view {
			width: 450rpx;
			height: 450rpx;
			// background-position:460rpx 520rpx;
		}
		image {
			width: 100%;
			height: 100%;
		}
	}

	.conterBg {
		width: 650rpx;
		height: 1000rpx;
		background-repeat: no-repeat;
		background-size: cover;
		margin: 80rpx auto;
		border-radius: 20rpx;
		padding: 0 30rpx;
		padding-top: 50rpx; 
	}

	.centerTxt {
		text-align: center;
	}
</style>
